import re
from itertools import zip_longest

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 设置ChromeDriver的路径（根据你的实际路径来设置）
# chromedriver_path = '/path/to/your/chromedriver'

# 初始化浏览器驱动
driver = webdriver.Chrome()

# 打开目标网页
driver.maximize_window()
driver.get('http://meps-k.com/')
wait=WebDriverWait(driver,5)
# 关闭注册弹窗
element_path='.iconfont.icon-shouyedanchuang-guanbi'
element=wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,element_path)))
element.click()
# 关闭优惠券弹窗
element_path='.iconfont.icon-danchuang-guanbi'
element=wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,element_path)))
element.click()
# 获取页面HTML内容
content = driver.page_source
#re获取商品名称
# re获取商品名称
pattern_name = r'h-\[34px\]">(.*?)</div'
goods_name_list = re.findall(pattern_name, content, flags=re.S)
# 接受循环后的商品名称
goods_name = []
get_goods_name = [s.replace('\n', '').strip() for s in goods_name_list]
for i in get_goods_name:
    goods_name_temp = i.find('>')
    if goods_name_temp != 1:
        goods_name_temp1 = i[goods_name_temp + 1:]
        goods_name.append(goods_name_temp1)

# 获取商品销售价
pattern_sell_price = r'class="text-\[#3C3C3C\] font-bold text-\[14px\]">(.*?)</span>'
goods_price_list = re.findall(pattern_sell_price, content, flags=re.S)
# print(goods_price_list)
sell_of_goods = [s.replace('\n', '') for s in goods_price_list]
sell_of_goods = [s.replace(' ', '') for s in sell_of_goods]
# print(sell_of_goods)
# 获取商品原价
pattern_cost_price = r'class="text-\[#85878C\] ml-10px text-\[12px\]">(.*?)</del>'
goods_price_list = re.findall(pattern_cost_price, content, flags=re.S)
# print(goods_price_list)
cost_of_goods = [s.replace('\n', '') for s in goods_price_list]
cost_of_goods = [s.replace(' ', '') for s in cost_of_goods]
for goods_name , sell_of_goods,cost_of_goods  in zip_longest (goods_name,sell_of_goods,cost_of_goods):
    if not cost_of_goods:
        cost_of_goods=sell_of_goods
    print('商品名称：{}，商品销售价：{}，商品成本价：{}'.format(goods_name,sell_of_goods,cost_of_goods))
# 关闭浏览器驱动
driver.quit()